import tkinter

import cv2
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk

def browse_video():
    global video_path
    # video_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4;*.avi;*.mkv;")])
    video_path = filedialog.askopenfilename()
    if video_path:
        update_video_info()
        preview_frame()

def update_video_info():
    cap = cv2.VideoCapture(video_path)
    #宽高
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    # 设置当前所处在的帧位置
    cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
    #总帧数
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #帧率
    fps = cap.get(cv2.CAP_PROP_FPS)
    # 当前帧的时间戳
    current_frame_msec = cap.get(cv2.CAP_PROP_POS_MSEC)
    #总时长
    duration = total_frames / fps

    show_frame = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
    cap.release()
    video_info_label.config(text=f'''总时长：{duration:.2f}秒 总帧数：{total_frames} 帧率：{fps:.2f} 分辨率{width:.0f}x{height:.0f}
                                        当前帧：{show_frame}  msec:{current_frame_msec:.4f}''')




def preview_frame():
    cap = cv2.VideoCapture(video_path)
    # 获取视频的宽度和高度
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
    preview_frame = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
    print(f"{preview_frame}")


    ret, frame = cap.read()
    cap.release()

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    image = Image.fromarray(frame)
    image = image.resize((1920, 1080), Image.AFFINE)
    photo = ImageTk.PhotoImage(image)
    lab.config(image=photo)
    lab.image = photo

def prev_frame():
    global current_frame
    current_frame -= 1
    update_video_info()
    preview_frame()

def next_frame():
    global current_frame
    print("next frame")
    current_frame += 1
    update_video_info()
    preview_frame()

def select_frame():
    if video_path:
        global current_frame
        print("next frame")
        current_frame =int(entry.get())
        update_video_info()
        preview_frame()

def mark_frame():
    global current_frame_msec
    record_array.append("第" + str(current_frame) + "帧，时间：" + str(current_frame_msec)+"\n")
    text.delete(1.0, tkinter.END)
    for s in record_array:
        text.insert('1.0',s)


record_array=[]
text_array = []

root = Tk()
root.title("视频预览")
root.geometry("1920x1080")


video_path = ""
current_frame = 0
current_frame_msec = 0

browse_button = Button(root, text="浏览视频", command=browse_video)
browse_button.pack()

video_info_label = Label(root, text="")
video_info_label.pack()

lab = Label(root, width=1920, height=1080)
lab.pack()


prev_button = Button(root, text="上一帧", command=prev_frame)
prev_button.pack(side=LEFT)

next_button = Button(root, text="下一帧", command=next_frame)
next_button.pack(side=RIGHT)

entry = Entry(root)
entry.pack(side=BOTTOM)

sure_button = Button(root, text="确定", command=select_frame)
sure_button.pack(side=BOTTOM)

record_button =Button(root,text="标记",command=mark_frame)
record_button.pack(side=LEFT)

text = tkinter.Text(root, wrap="word",width=20)
text.pack(side=LEFT)


root.mainloop()
